using UnityEngine;

public class Attack2 : MonoBehaviour
{
	public Rigidbody projectile;

	public int force = 100;

	private bool end;

	private int touchIndex;

	private Vector2 touchStart;

	private Vector2 touchEnd;

	private Camera cam;

	private Light mainlight;

	private Color defaultLightColor;

	private void Start()
	{
		mainlight = GameObject.Find("Directional light").GetComponent<Light>();
		defaultLightColor = mainlight.color;
		StateManager.heightLimit = 6;
		StateManager.specialAttack = true;
		LeanTween.value(mainlight.gameObject, mainlight.color, new Color(0.88f, 0.76f, 0.76f), 0.8f).setOnUpdate(delegate(Color val)
		{
			mainlight.color = val;
		}).setEase(LeanTweenType.easeInOutCubic);
		base.transform.parent = GameObject.FindGameObjectWithTag("Player").transform;
		base.transform.localPosition = new Vector3(0f, 1f, 0f);
		cam = Camera.main;
	}

	private void Update()
	{
		if (end)
		{
			return;
		}
		int touchCount = UnityEngine.Input.touchCount;
		for (int i = 0; i < touchCount; i++)
		{
			Touch touch = UnityEngine.Input.GetTouch(i);
			if (touch.phase == TouchPhase.Began)
			{
				touchStart = touch.position;
				touchEnd = touchStart;
				touchIndex = i;
			}
			else if (i == touchIndex)
			{
				touchEnd = touch.position;
			}
			if (i == touchIndex && touch.phase == TouchPhase.Ended)
			{
				if (!(touchEnd.x / touchStart.x > 0.89f) || !(touchEnd.x / touchStart.x < 1.11f) || !(touchEnd.y / touchStart.y > 0.89f) || !(touchEnd.y / touchStart.y < 1.11f))
				{
					break;
				}
				Attack(touchEnd);
			}
		}
	}

	private void Attack(Vector2 touchPosition)
	{
		if (Physics.Raycast(cam.ScreenPointToRay(touchPosition), out RaycastHit hitInfo, 1500f))
		{
			LeanTween.value(mainlight.gameObject, mainlight.color, defaultLightColor, 0.1f).setOnUpdate(delegate(Color val)
			{
				mainlight.color = val;
			});
			LeanTween.value(mainlight.gameObject, mainlight.color, new Color(0.88f, 0.76f, 0.76f), 0.3f).setOnUpdate(delegate(Color val)
			{
				mainlight.color = val;
			}).setDelay(0.2f);
			Vector3 vector = Vector3.Normalize(hitInfo.point - cam.transform.position);
			Rigidbody rigidbody = UnityEngine.Object.Instantiate(projectile, cam.transform.position + Vector3.up, Quaternion.LookRotation(vector));
			rigidbody.AddForce(vector * force, ForceMode.Impulse);
			StateManager.specialEnergy = Mathf.Clamp(StateManager.specialEnergy - 18f, 0f, 100f);
			if (StateManager.specialEnergy <= 5f)
			{
				EndAttack();
			}
		}
	}

	private void EndAttack()
	{
		end = true;
		EndEffects();
	}

	private void EndEffects()
	{
		LeanTween.value(mainlight.gameObject, mainlight.color, defaultLightColor, 0.8f).setOnUpdate(delegate(Color val)
		{
			mainlight.color = val;
		}).setEase(LeanTweenType.easeInOutCubic);
		UnityEngine.Object.Destroy(base.gameObject);
		StateManager.heightLimit = 3;
		StateManager.specialAttack = false;
	}
}
